package cn.yunhe.service;

import cn.yunhe.dao.FboardDao;
import cn.yunhe.dao.SboardDao;
import cn.yunhe.entity.Fboard;
import cn.yunhe.entity.Sboard;
import cn.yunhe.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class SboardService {
    SqlSession session=null;
    SboardDao sboardDao=null;
    FboardDao fboardDao=null;
    /**
     * 初始化&&反射
     */
    public void initial(){
        session=SqlSessionFactoryUtil.getSqlSession();
        sboardDao=session.getMapper(SboardDao.class);
        fboardDao=session.getMapper(FboardDao.class);
    }
    /**
     * 提交&&销毁
     */
    public void destory(){
        session.commit();
        if (session!=null){
            session.close();
        }
    }

    /**
     * 根据二级版块的sbid获取完整的 二级版块信息
     * @param sbid
     * @return
     */
    public Sboard searchSboardBySbid(int sbid){
        initial();
        Sboard sboard=sboardDao.searchSboardBySbid(sbid);
        destory();
        return sboard;
    }

    /**
     * 根据二级版块的sbname获取完整的 二级版块信息
     * @param sbname
     * @return
     */
    public Sboard searchSboardBySbname(String sbname){
        initial();
        Sboard sboard=sboardDao.searchSboardBySbname(sbname);
        destory();
        return sboard;
    }

    /**
     * 新增二级版块
     * @param fbid
     * @param sbname
     * @return
     */
    public boolean addSboard(int fbid,String sbname){
        initial();
        //s1:
        Fboard fboard=fboardDao.searchFboardByFbId(fbid);
        //s2:
        Sboard sboard=new Sboard();
        sboard.setSbname(sbname);
        sboard.setFboard(fboard);
        boolean flag=sboardDao.addSboard(sboard);
        destory();
        return flag;

    }

    /**
     * 更新某一个二级版块的名称
     * @param sbid
     * @param sbname
     * @return
     */
    public boolean updateSboard(int sbid,String sbname){
        initial();
        //s1
        Sboard sboard=new Sboard();
        sboard.setSbid(sbid);
        sboard.setSbname(sbname);
        //s2
        boolean flag=sboardDao.updateSboard(sboard);
        destory();
        return flag;
    }

    /**
     * 删除某一个二级版块
     * @param sbid
     * @return
     */
    public boolean deleteSboard(int sbid){
        initial();
        int sbflag=-1;
        //s1
        Sboard  sboard=sboardDao.searchSboardBySbid(sbid);
        //s2
        if (sboard.getSbflag()==0){
            sbflag=1;
        }else if (sboard.getSbflag()==1){
            sbflag=0;
        }
        sboard.setSbflag(sbflag);
        //s3
        boolean flag=sboardDao.deleteSboard(sboard);
        destory();
        return flag;
    }

    /**
     *根据sbflag获取 所有相关的二级版块列表
     * sbflag:0-显示,1-隐藏
     *当sbflag<0时,查询所有二级版块
     * 连表查询,t_fboard,t_sboard
     * @param sbflag
     * @return
     */
    public List<Sboard> findSboardBySbflag(int sbflag){
        initial();
        //s1
        Sboard sboard=new Sboard();
        sboard.setSbflag(sbflag);
        //s2
        List<Sboard>sboardList=sboardDao.findSboardBySbflag(sboard);
        //s3
        destory();
        return  sboardList;
    }
    /**
     * 根据fbid获取 所有相关的二级版块列表
     * 当fbid<0时,查询所有二级版块
     * 连表查询,t_fboard,t_sboard
     * @param fbid
     * @return
     */
    public List<Sboard> findSboardByFbid(int fbid){
        initial();
        //s1
        Fboard fboard=new Fboard();
        fboard.setFbid(fbid);
        //s2
        Sboard sboard=new Sboard();
        sboard.setFboard(fboard);
        //s3
        List<Sboard>sboardList=sboardDao.findSboardByFbid(sboard);
        //s4
        destory();
        return  sboardList;
    }

    /**
     * 获取二级菜单
     * @param fbid
     * @return
     */
    public List<Sboard> getSboardByFbid(int fbid) {
        initial();
        List<Sboard> sboardList = sboardDao.getSboardByFbid(fbid);
        destory();
        return  sboardList;
    }






}
